/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 123
 * Date: 2023-05-22
 * Time: 20:54
 */
public class MyLinkedList {
    private static  final int useSize = 10 ;
    private ListNode head ;
    private ListNode last ;
    static class ListNode{
        private int key ;
        private ListNode prev ;
        private ListNode next ;
        public ListNode(int data){
            this.key = data ;
        }


    }

        //头插法
        public void addFirst(int data){
        ListNode Node = new ListNode(data) ;
        if(head == null ){
            head = last =Node ;
            return;
        }
            Node.next = head ;
            head.prev = Node ;
            head = Node ;


        }
        //尾插法
        public void addLast(int data){
        ListNode node = new ListNode(data) ;
        if(last == null ){
            last = head = node ;
            return;
        }

        last.next = node ;
        node.prev = last ;
        last = node ;

        }
        //任意位置插入,第一个数据节点为0号下标
        public void addIndex(int index,int data){
         if(index <0 ||index > size()){
             System.out.println(index +" 位置不合法");
             return ;
         }
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }
            ListNode node = new ListNode(data) ;
            ListNode cur = head ;
            while (index - 1 != 0 ){
                cur = cur .next ;
                index-- ;
            }
            node .prev = cur ;
            node.next = cur.next ;
            cur.next.prev= node ;
             cur.next= node ;
        }
//        //查找是否包含关键字key是否在单链表当中
//        public boolean contains(int key){}
//        //删除第一次出现关键字为key的节点
//        public void remove(int key){}
//        //删除所有值为key的节点
//        public void removeAllKey(int key){}
//        //得到单链表的长度
        public int size() {
            ListNode cur = this.head;
            int count = 0;
            while (cur != null) {
                count++;
                cur = cur.next;
            }
            return count ;
        }
         public void display(){
        ListNode cur = this .head ;
        while(cur != null){
            System.out.print(cur.key + "  ");
            cur = cur.next ;
        }
             System.out.println();
         }



    //        public void clear(){}
//


}
